package com.xgkj.diyiketang.activity.data;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.HisDetailActivity;
import com.xgkj.diyiketang.activity.NewFriendActivity;
import com.xgkj.diyiketang.data.EatShowUserInfo;
import com.xgkj.diyiketang.util.ImageCache;
import com.xgkj.diyiketang.util.ViewUtil;
import com.xgkj.diyiketang.weidget.RoundImageView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by windo on 16/8/2.
 */
public class NewFriendAdapter extends BaseAdapter{
    private Context mContext;
    private List<EatShowUserInfo> mList = new ArrayList<EatShowUserInfo>();

    public NewFriendAdapter(Context context) {
        mContext = context;
    }

    public void updateData(List<EatShowUserInfo> list){
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public void clearData(){
        mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        if (mList.size()>0){
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.newfriendlist_item, null);
            convertView.setTag(new ViewHolder(convertView));
        }
        EatShowUserInfo info = mList.get(position);
        if (info != null) {
            initializeViews(info, (ViewHolder) convertView.getTag());
        }

        return convertView;
    }

    private void initializeViews(final EatShowUserInfo info, ViewHolder holder) {
        String headUrl = info.getUserLogo();
        if (headUrl != null && headUrl.length() > 0){
            ViewUtil.Size size = ViewUtil.getHeadImageSize(mContext,40);
            String cache_key = ImageCache.getInstance(mContext).getCacheKey(headUrl,size);
            Bitmap cache_bitmap = ImageCache.getInstance(mContext).getBitmapFromMemoryCache(cache_key);
            if (cache_bitmap != null){
                holder.headImg.setImageBitmap(cache_bitmap);
            }else {
                holder.headImg.setImageResource(R.drawable.default_head_image);
                holder.headImg.setTag(headUrl);
                ImageCache.getInstance(mContext).loadBitmapToView(holder.headImg,headUrl,size);
            }
        }else {
            holder.headImg.setImageResource(R.drawable.default_head_image);
        }
//        holder.headImg.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                String uid = info.getUserId();
//                Intent intent = new Intent(mContext, HisDetailActivity.class);
//                intent.putExtra("USERID", uid);
//                mContext.startActivity(intent);
//            }
//        });
        try {
            holder.nickname.setText(URLDecoder.decode(info.getNickName(),"utf-8"));
            holder.personSignature.setText(URLDecoder.decode(info.getUserSign(),"utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        holder.receiveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null){
                    mListener.onReceiveClick(info.getUserId());
                }
            }
        });

        if (info.getState() == 0){
            holder.receiveBtn.setVisibility(View.INVISIBLE);
            holder.alreadyReceive.setVisibility(View.VISIBLE);
            holder.alreadyReceive.setText(R.string.already_apply);
        }else if (info.getState() == 1){
            holder.receiveBtn.setVisibility(View.INVISIBLE);
            holder.alreadyReceive.setVisibility(View.VISIBLE);
            holder.alreadyReceive.setText(R.string.already_receive);
        }else if (info.getState() ==2){
            holder.receiveBtn.setVisibility(View.INVISIBLE);
            holder.alreadyReceive.setVisibility(View.VISIBLE);
            holder.alreadyReceive.setText(R.string.already_refuse);
        }else if (info.getState() ==3){
            holder.receiveBtn.setVisibility(View.VISIBLE);
            holder.alreadyReceive.setVisibility(View.GONE);
        }

    }

    public interface OnAdapterClickListener{
        public void onReceiveClick(String userId);
    }

    private OnAdapterClickListener mListener;

    public void setOnAdapterClickListener(NewFriendActivity listener){
        mListener = listener;
    }


    protected class ViewHolder {
        private RoundImageView headImg;
        private ImageButton receiveBtn;
        private TextView alreadyReceive;
        private TextView nickname;
        private TextView personSignature;

        public ViewHolder(View view) {
            headImg = (RoundImageView) view.findViewById(R.id.head_img);
            receiveBtn = (ImageButton) view.findViewById(R.id.receive_btn);
            alreadyReceive = (TextView) view.findViewById(R.id.already_receive);
            nickname = (TextView) view.findViewById(R.id.nickname);
            personSignature = (TextView) view.findViewById(R.id.person_signature);
        }
    }
}
